# Copyright 2010-2025 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

package(default_visibility = ["//visibility:public"])

config_setting(
    name = "on_linux",
    constraint_values = [
        "@platforms//os:linux",
    ],
)

config_setting(
    name = "on_macos",
    constraint_values = [
        "@platforms//os:macos",
    ],
)

config_setting(
    name = "on_windows",
    constraint_values = [
        "@platforms//os:windows",
    ],
)

# Floating-point code in this directory must not be compiled with
# dangerous optimizations. For example do not assume that FP expressions
# are associative. This is what -fno-fast-math is for.
SAFE_FP_CODE = select({
    "on_linux": ["-fno-fast-math"],
    "on_macos": [],  # no_fast_math is the default.
    "on_windows": [],  # /fp:precise is the default.
    "//conditions:default": [],
})

cc_library(
    name = "base",
    srcs = ["lp_types.cc"],
    hdrs = ["lp_types.h"],
    deps = [
        "//ortools/base",
        "//ortools/base:hash",
        "//ortools/base:strong_vector",
        "//ortools/util:bitset",
        "//ortools/util:strong_integers",
    ],
)

cc_library(
    name = "permutation",
    hdrs = ["permutation.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        "//ortools/base",
        "//ortools/util:return_macros",
        "@com_google_absl//absl/random",
    ],
)

# Vectors that offer a sparse interface to dense storage.
cc_library(
    name = "scattered_vector",
    hdrs = ["scattered_vector.h"],
    deps = [
        ":base",
        "//ortools/base",
        "//ortools/base:strong_vector",
        "//ortools/util:bitset",
        "//ortools/util:strong_integers",
    ],
)

cc_library(
    name = "sparse_vector",
    hdrs = ["sparse_vector.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        ":permutation",
        "//ortools/base",
        "//ortools/graph:iterators",
        "//ortools/util:return_macros",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
    ],
)

cc_library(
    name = "sparse_column",
    srcs = ["sparse_column.cc"],
    hdrs = ["sparse_column.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        ":sparse_vector",
        "//ortools/base",
    ],
)

cc_library(
    name = "sparse_row",
    hdrs = ["sparse_row.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        ":sparse_vector",
        "//ortools/base",
    ],
)

cc_library(
    name = "sparse",
    srcs = ["sparse.cc"],
    hdrs = [
        "sparse.h",
    ],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        ":matrix_scaler_hdr",
        ":permutation",
        ":scattered_vector",
        ":sparse_column",
        "//ortools/base",
        "//ortools/base:hash",
        "//ortools/base:strong_vector",
        "//ortools/util:fp_utils",
        "//ortools/util:return_macros",
        "//ortools/util:strong_integers",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "matrix_scaler",
    srcs = ["matrix_scaler.cc"],
    hdrs = ["matrix_scaler.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        ":lp_utils",
        ":sparse",
        "//ortools/base",
        "//ortools/base:hash",
        "//ortools/base:strong_vector",
        "//ortools/glop:parameters_cc_proto",
        "//ortools/glop:revised_simplex",
        "//ortools/glop:status",
        "//ortools/util:fp_utils",
    ],
)

cc_library(
    name = "matrix_scaler_hdr",
    hdrs = ["matrix_scaler.h"],
    deps = [
        ":base",
        "//ortools/base",
        "//ortools/base:strong_vector",
    ],
)

cc_library(
    name = "lp_data",
    srcs = ["lp_data.cc"],
    hdrs = ["lp_data.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        ":lp_print_utils",
        ":lp_utils",
        ":matrix_utils",
        ":permutation",
        ":sparse",
        "//ortools/base",
        "//ortools/base:hash",
        "//ortools/base:strong_vector",
        "//ortools/glop:parameters_cc_proto",
        "//ortools/util:fp_utils",
        "//ortools/util:strong_integers",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "lp_data_utils",
    srcs = ["lp_data_utils.cc"],
    hdrs = ["lp_data_utils.h"],
    deps = [
        ":base",
        ":lp_data",
        ":matrix_scaler",
        "//ortools/glop:parameters_cc_proto",
    ],
)

cc_library(
    name = "lp_utils",
    srcs = ["lp_utils.cc"],
    hdrs = ["lp_utils.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        ":scattered_vector",
        ":sparse_column",
        "//ortools/base",
        "//ortools/base:accurate_sum",
    ],
)

cc_library(
    name = "matrix_utils",
    srcs = ["matrix_utils.cc"],
    hdrs = ["matrix_utils.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        ":sparse",
        "//ortools/base",
        "//ortools/base:hash",
    ],
)

cc_library(
    name = "lp_parser",
    srcs = ["lp_parser.cc"],
    hdrs = ["lp_parser.h"],
    copts = SAFE_FP_CODE,
    defines = ["USE_LP_PARSER"],
    deps = [
        ":base",
        ":lp_data",
        ":proto_utils",
        "//ortools/base",
        "//ortools/base:case",
        "//ortools/base:map_util",
        "//ortools/linear_solver:linear_solver_cc_proto",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_re2//:re2",
    ],
)

#cc_library(
#    name = "lp_constraint_classifier",
#    srcs = ["lp_constraint_classifier.cc"],
#    hdrs = ["lp_constraint_classifier.h"],
#    copts = SAFE_FP_CODE,
#    deps = [
#        ":lp_data",
#        "//ortools/util:fp_utils",
#    ],
#)

cc_library(
    name = "lp_print_utils",
    srcs = ["lp_print_utils.cc"],
    hdrs = ["lp_print_utils.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        "//ortools/base",
        "//ortools/util:rational_approximation",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
    ],
)

cc_library(
    name = "proto_utils",
    srcs = ["proto_utils.cc"],
    hdrs = ["proto_utils.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        ":lp_data",
        "//ortools/base",
        "//ortools/linear_solver:linear_solver_cc_proto",
    ],
)

cc_library(
    name = "mps_reader_template",
    srcs = ["mps_reader_template.cc"],
    hdrs = ["mps_reader_template.h"],
    deps = [
        "//ortools/base",
        "//ortools/base:map_util",
        "//ortools/base:status_macros",
        "//ortools/util:filelineiter",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/container:inlined_vector",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "mps_reader",
    srcs = ["mps_reader.cc"],
    hdrs = ["mps_reader.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":lp_data",
        ":lp_print_utils",
        ":mps_reader_template",
        "//ortools/base",
        "//ortools/base:protobuf_util",
        "//ortools/base:status_builder",
        "//ortools/base:status_macros",
        "//ortools/base:strong_vector",
        "//ortools/linear_solver:linear_solver_cc_proto",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "model_reader",
    srcs = ["model_reader.cc"],
    hdrs = ["model_reader.h"],
    deps = [
        ":lp_data",
        ":mps_reader",
        ":proto_utils",
        #        "//net/proto2/util/public:differencer",
        "//ortools/base",
        "//ortools/base:file",
        "//ortools/linear_solver:linear_solver_cc_proto",
        "//ortools/util:file_util",
    ],
)

cc_library(
    name = "lp_decomposer",
    srcs = ["lp_decomposer.cc"],
    hdrs = ["lp_decomposer.h"],
    copts = SAFE_FP_CODE,
    deps = [
        ":base",
        ":lp_data",
        ":lp_utils",
        "//ortools/algorithms:dynamic_partition",
        "//ortools/base",
        "//ortools/base:hash",
        "//ortools/glop:parameters_cc_proto",
        "@com_google_absl//absl/synchronization",
    ],
)

cc_library(
    name = "sol_reader",
    srcs = ["sol_reader.cc"],
    hdrs = ["sol_reader.h"],
    deps = [
        ":base",
        ":lp_data",
        ":proto_utils",
        "//ortools/base",
        "//ortools/base:numbers",
        "//ortools/linear_solver:linear_solver_cc_proto",
        "//ortools/util:file_util",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)
